﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHapi.Base.Parser;
//using NHapi.Model.V231;
//using NHapi.Model.V231.Message;
//using NHapi.Model.V231.Datatype;
using NHapi.Base.Model;
//using NHapi.Model.V231.Segment;



namespace TcpIpServerTest
{
    public class HL7Samples
    {
        /*
        public static String QRY_R02_SampleMessage()
        {
            NHapi.Base.Parser.PipeParser parser = new NHapi.Base.Parser.PipeParser();
            QRY_R02 qry = new QRY_R02();
            qry.MSH.MessageType.MessageType.Value = "QRY";
            qry.MSH.MessageType.TriggerEvent.Value = "R02";
            qry.MSH.MessageType.MessageStructure.Value = "QRY_R02";
            qry.MSH.FieldSeparator.Value = "|";
            qry.MSH.SendingApplication.NamespaceID.Value = "CohieCentral";
            qry.MSH.SendingFacility.NamespaceID.Value = "COHIE";
            qry.MSH.ReceivingApplication.NamespaceID.Value = "Clinical Data Provider";
            qry.MSH.ReceivingFacility.NamespaceID.Value = "";  //facility;
            qry.MSH.EncodingCharacters.Value = @"^~\&";
            qry.MSH.VersionID.VersionID.Value = "2.3.1";
            qry.MSH.DateTimeOfMessage.TimeOfAnEvent.SetLongDate(DateTime.Now);
            qry.MSH.MessageControlID.Value = "";// messageControlId;
            qry.MSH.ProcessingID.ProcessingID.Value = "P";

            XCN st = qry.QRD.GetWhoSubjectFilter(0);// qry.QRD.getWhoSubjectFilter(0);
            st.AssigningAuthority.UniversalID.Value = "";// facility;
            st.IDNumber.Value = "";//mrn;

            qry.QRD.QueryDateTime.TimeOfAnEvent.SetLongDate(DateTime.Now);
            qry.QRD.QueryFormatCode.Value = "R";
            qry.QRD.QueryPriority.Value = "I";
            CE what = qry.QRD.GetWhatSubjectFilter(0);// getWhatSubjectFilter(0);
            what.Identifier.Value = "RES";


            DefaultXMLParser parser1 = new DefaultXMLParser();
            return parser1.Encode(qry);




            return parser.Encode(qry);//  .encode(qry);
        }
        */

        /*
        public static String temp()
        {

            string message = @"MSH|^~\&|SENDING|SENDER|RECV|INST|20060228155525|
            |QRY^R02^QRY_R02|1|P|2.3|QRD|20060228155525|R|I||||10^RD&Records&0126|38923^^^^^^^^&INST|||";

            PipeParser parser = new PipeParser();
            IMessage m = parser.Parse(message);

            QRY_R02 qryR02 = m as QRY_R02;
            if (qryR02 == null) return "";

            return qryR02.QRD.GetWhoSubjectFilter(0).IDNumber.Value + "";
        }
         * */
        /*

        public static String temp2()
        {

            ADT_A01 adt = new ADT_A01();

            // Populate the MSH Segment
            MSH mshSegment = adt.MSH;// getMSH();
            mshSegment.FieldSeparator.Value = "|";// getFieldSeparator().setValue("|");
            mshSegment.EncodingCharacters.Value = "^~\\&";// getEncodingCharacters().setValue("^~\\&");
            mshSegment.DateTimeOfMessage.TimeOfAnEvent.Value = "200701011539";//  getDateTimeOfMessage().getTimeOfAnEvent().setValue("200701011539");
            mshSegment.SendingApplication.NamespaceID.Value = "TestSendingSystem";// getSendingApplication().getNamespaceID().setValue("TestSendingSystem");            
            mshSegment.SequenceNumber.Value = "123";// getSequenceNumber().setValue("123");
            mshSegment.MessageType.MessageType.Value = "ADT";// getMessageType().getMessageType().setValue("ADT");
            mshSegment.MessageType.TriggerEvent.Value = "A01"; //getMessageType().getTriggerEvent().setValue("A01");
            mshSegment.MessageType.MessageStructure.Value = "ADT A01";// getMessageType().getMessageStructure().setValue("ADT A01");

            // Populate the PID Segment
            PID pid = adt.PID;// getPID(); 
            //pid.getPatientName(0).getFamilyName().getSurname().setValue("Doe");
            pid.GetPatientName(0).FamilyLastName.FamilyName.Value = "Doe";
            //pid.getPatientName(0).getGivenName().setValue("John");
            pid.GetPatientName(0).GivenName.Value = "John";
            //pid.getPatientIdentifierList(0).getID().setValue("123456");
            pid.GetPatientIdentifierList(0).ID.Value = "123456";


            // Now, let's encode the message and look at the output
            ParserBase parser = new NHapi.Base.Parser.PipeParser();
            String encodedMessage = parser.Encode(adt);

            parser = new DefaultXMLParser();


            return parser.Encode(adt);
        }
        */

        public static String parseMessageXML(String message) {
            ParserBase parser = new PipeParser();
            IMessage m = parser.Parse(message);
            if (m == null) return "";
            parser = new DefaultXMLParser();            
            return  parser.Encode(m);
        }
         public static String parseMessageXML23(String message) {
            ParserBase parser = new PipeParser();
            IMessage m = parser.Parse(message);
            if (m == null) return "";
            parser = new DefaultXMLParser();
            return  parser.Encode(m);
        }
        
    }


}
